package com.ruoyi.project.jiaqin.subject.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.jiaqin.industry.domain.JiaqinIndustry;
import com.ruoyi.project.jiaqin.industry.service.IJiaqinIndustryService;
import com.ruoyi.project.jiaqin.subject.domain.JiaqinSubject;
import com.ruoyi.project.jiaqin.subject.service.IJiaqinSubjectService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 话题Controller
 *
 * @author admin
 * @date 2020-12-30
 */
@Controller
@RequestMapping("/jiaqin/subject")
public class JiaqinSubjectController extends BaseController
{
    private String prefix = "jiaqin/subject";

    @Autowired
    private IJiaqinSubjectService jiaqinSubjectService;

    @Autowired
    private IJiaqinIndustryService iJiaqinIndustryService;

    @RequiresPermissions("jiaqin:subject:view")
    @GetMapping()
    public String subject()
    {
        return prefix + "/subject";
    }

    /**
     * 查询话题列表
     */
    @RequiresPermissions("jiaqin:subject:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JiaqinSubject jiaqinSubject)
    {
        startPage();
        List<JiaqinSubject> list = jiaqinSubjectService.selectJiaqinSubjectList(jiaqinSubject);
        return getDataTable(list);
    }

    /**
     * 导出话题列表
     */
    @RequiresPermissions("jiaqin:subject:export")
    @Log(title = "话题", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JiaqinSubject jiaqinSubject)
    {
        List<JiaqinSubject> list = jiaqinSubjectService.selectJiaqinSubjectList(jiaqinSubject);
        ExcelUtil<JiaqinSubject> util = new ExcelUtil<JiaqinSubject>(JiaqinSubject.class);
        return util.exportExcel(list, "subject");
    }

    /**
     * 新增话题
     */
    @GetMapping("/add")
    public String add(Model model)
    {
        model.addAttribute("industryList",iJiaqinIndustryService.selectJiaqinIndustryList(new JiaqinIndustry()));
        return prefix + "/add";
    }

    /**
     * 新增保存话题
     */
    @RequiresPermissions("jiaqin:subject:add")
    @Log(title = "话题", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(JiaqinSubject jiaqinSubject)
    {
        return toAjax(jiaqinSubjectService.insertJiaqinSubject(jiaqinSubject));
    }

    /**
     * 修改话题
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        JiaqinSubject jiaqinSubject = jiaqinSubjectService.selectJiaqinSubjectById(id);
        mmap.put("jiaqinSubject", jiaqinSubject);
        mmap.put("industryList",iJiaqinIndustryService.selectJiaqinIndustryList(new JiaqinIndustry()));
        return prefix + "/edit";
    }

    /**
     * 修改保存话题
     */
    @RequiresPermissions("jiaqin:subject:edit")
    @Log(title = "话题", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JiaqinSubject jiaqinSubject)
    {
        return toAjax(jiaqinSubjectService.updateJiaqinSubject(jiaqinSubject));
    }

    /**
     * 删除话题
     */
    @RequiresPermissions("jiaqin:subject:remove")
    @Log(title = "话题", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jiaqinSubjectService.deleteJiaqinSubjectByIds(ids));
    }
}
